package com.gperp.service.impl;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.gperp.common.GPResult;
import com.gperp.controller.LogAspect;
import com.gperp.mapper.AsKeywordsMapper;
import com.gperp.mapper.AsSystemconfigMapper;
import com.gperp.pojo.AsAccountdetail;
import com.gperp.pojo.AsKeywords;
import com.gperp.pojo.AsSystemconfig;
import com.gperp.pojo.AsSystemconfigExample;
import com.gperp.pojo.AsUser;
import com.gperp.service.AccountDetailService;
import com.gperp.service.SystemService;

@Service
public class SystemServcieImpl implements SystemService{
	@Resource
	private AsSystemconfigMapper systemMapper;
	@Resource
	private AccountDetailService accountDetailService;
	@Autowired
    private LogAspect aspect;
	@Resource
	private AsKeywordsMapper asKeywordsMapper;
	
	@Override
	public List<AsSystemconfig> querySystemAll() {
		AsSystemconfigExample example = new AsSystemconfigExample();
		example.createCriteria().andConfigtypeEqualTo(1);//操作账号默认1
		return systemMapper.selectByExample(example);
	}

	@Override
	public GPResult updatexufei(Long id, Integer type, Integer year,Double price) {
		try {
			AsKeywords asKeywords=asKeywordsMapper.selectByPrimaryKey(id);
			
			//扣费
			AsAccountdetail accountDetail = new AsAccountdetail();
			AsUser user= aspect.getUser();
			accountDetail.setMemo("用户"+user.getUsername()+"对关键词‘"+asKeywords.getKeywords()+"’进行续费");
			accountDetail.setMoney(price-price*2);//取反
			accountDetail.setDetailtypename("消费");
			accountDetail.setUserid(user.getId());
			accountDetailService.saveAccount(accountDetail);
			Calendar rightNow = Calendar.getInstance();
			rightNow.setTime(asKeywords.getRegpassdatetime());
			rightNow.add(Calendar.YEAR,year);
			asKeywords.setRegpassdatetime(rightNow.getTime());
			asKeywords.setServiceyears(asKeywords.getServiceyears()+year);
			asKeywords.setProducttype(type);
			asKeywords.setPrice(asKeywords.getPrice()-price);
			asKeywordsMapper.updateByPrimaryKeySelective(asKeywords);
		} catch (Exception e) {
			e.printStackTrace();
			return GPResult.build(200, "NO");
		}
		return GPResult.ok();
	}

}
